package cn.tedu.boot31.controller;

import cn.tedu.boot31.entity.Emp;
import cn.tedu.boot31.utils.DBUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

//@Controller
@RestController //相当于每个处理请求的方法上都添加了一个@@ResponseBody注解
public class EmpController {
    @RequestMapping("/add")
    public String add(Emp emp){
        try (Connection conn = DBUtils.getConn()) {
            String sql = "insert into newemp values (null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            int i = ps.executeUpdate();
            if (i>0) {
                return "成功添加"+i+"条数据!<a href='javascript:history.go(-2)'>返回主页</a>";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return "添加失败!";
    }
    @RequestMapping("/select")
    public String select(){
        ArrayList<Emp> list = new ArrayList<>();
        try (Connection conn = DBUtils.getConn()) {
            String sql = "select * from newemp";
            Statement s = conn.createStatement();
            ResultSet rs = s.executeQuery(sql);
            while (rs.next()) {
                int id = rs.getInt(1);
                String name = rs.getString(2);
                String job = rs.getString(3);
                int sal = rs.getInt(4);
                Emp emp = new Emp(id,name,job,sal);
                list.add(emp);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        String html = "<table border=1>\n" +
                "   <caption>员工列表</caption>\n" +
                "   <tr>\n" +
                "       <th>id</th>\n" +
                "       <th>名字</th>\n" +
                "       <th>工作</th>\n" +
                "       <th>工资</th>\n" +
                "       <th>操作</th>\n" +
                "   </tr>";
        for (Emp e : list) {
            html += "   <tr>\n" +
                    "       <td>"+e.getId()+"</td>\n" +
                    "       <td>"+e.getName()+"</td>\n" +
                    "       <td>"+e.getJob()+"</td>\n" +
                    "       <td>"+e.getSal()+"</td>\n" +
                    "       <td><a href='/delete?id="+e.getId()+"'>删除</a></td>\n" +
                    "   </tr>";
        }
        html += "</table>";
        return html;
    }
    @RequestMapping("/delete")
    public void delete(int id, HttpServletResponse response) {
        try (Connection conn = DBUtils.getConn()) {
            String sql = "delete from newemp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            int i = ps.executeUpdate();
            if (i>0) {
                //执行流程:服务器会向客户端浏览器返回一个302状态码和一个地址
                //浏览器接收到302会立即向指定地址再次发出请求
                response.sendRedirect("/select");
//                return "成功删除"+i+"一条数据!<a href='/select'>返回列表</a>";
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        return "删除失败!<a href='/select'>返回列表</a>";
    }
    @RequestMapping("/update")
    public void update(Emp emp,HttpServletResponse response){
        try (Connection conn = DBUtils.getConn()) {
            String sql = "update newemp set name=?,job=?,sal=? where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setString(2,emp.getJob());
            ps.setInt(3,emp.getSal());
            ps.setInt(4,emp.getId());
            int i = ps.executeUpdate();
            if (i>0) {
                response.sendRedirect("/select");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
